package henu.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import henu.bean.Communictor;
import henu.bean.User;
import henu.dao.ICommunictorDao;
import henu.dao.IUserDao;
import henu.factory.DaoFactory;

public class CommAction extends ActionSupport{
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest request = ServletActionContext.getRequest();
//		request.setCharacterEncoding("GB2312");
		String action=request.getParameter("action");
		Communictor communictor=new Communictor();
		ICommunictorDao dao=DaoFactory.createCommunictorDao();
		//此入口的作用是从数据库中get所有记录的数据，展示到面板中
		if(action.equals("open")){
			HttpSession session=request.getSession();
			List<Communictor> communictors=null;
			communictors=dao.getAll();
			if(communictors!=null)
			{
				session.setAttribute("communictor",communictors);
				return "COMM";
			}
			else{
				return ERROR;
			}
			
		}
		//添加用户 添加成功后返回comm界面
		if(action.equals("add")){
			String number = request.getParameter("number");
			String name = request.getParameter("name");
			String password = request.getParameter("password");
			String gradeName = request.getParameter("gradeName");
			String QQ = request.getParameter("QQ");
			String email = request.getParameter("email");
			String tel = request.getParameter("tel");
			String subject = request.getParameter("subject");
			String memo = request.getParameter("memo");
			
			communictor.setfId(number);
			communictor.setfName(name);
			communictor.setfPassword(password);
			communictor.setfClass(gradeName);
			communictor.setfQQ(QQ);
			communictor.setfEmail(email);
			communictor.setfPhone(tel);
			communictor.setfMianSubject(subject);
			communictor.setfMemo(memo);
			System.out.println(communictor.getfId());
			dao.save(communictor);
			List<Communictor> communictors=null;
			HttpSession session=request.getSession();
			communictors=dao.getAll();
			if(communictors==null)
			{
				return ERROR;
			}
			else{
				session.setAttribute("communictor", communictors);
				return "COMM";
			}
		}
		//删除
		if(action.equals("delete")){
			String fId=request.getParameter("fId");
			System.out.println("delete："+fId);
			dao.delete(fId);
			HttpSession session=request.getSession();
			List<Communictor> communictors=null;
			communictors=dao.getAll();
			if(communictors!=null)
			{
				session.setAttribute("communictor",communictors);
				return "COMM";
			}
			else{
				return ERROR;
			}
		}
		//此入口的作用是返回修改记录的对话框，将其记录中的数据传递到对话框中
		if(action.equals("updatebefore")){
			String fId=null;
			HttpSession session=request.getSession();
			fId=request.getParameter("fId");
			System.out.println(fId);
			System.out.println(action+":"+fId);
			communictor=dao.get(fId);
			System.out.println(communictor.getfId());
			if(communictor!=null)
			{
				session.setAttribute("communictor", communictor);
				return "update";
			}
			else
			{
				return ERROR;
			}
		}
		//此入口为修改记录的实际操作
		if(action.equals("update"))
		{
			String number = request.getParameter("number");
			String name = request.getParameter("name");
			String password = request.getParameter("password");
			String gradeName = request.getParameter("gradeName");
			String QQ = request.getParameter("QQ");
			String email = request.getParameter("email");
			String tel = request.getParameter("tel");
			String subject = request.getParameter("subject");
			String memo = request.getParameter("memo");
			
			communictor.setfId(number);
			communictor.setfName(name);
			communictor.setfPassword(password);
			communictor.setfClass(gradeName);
			communictor.setfQQ(QQ);
			communictor.setfEmail(email);
			communictor.setfPhone(tel);
			communictor.setfMianSubject(subject);
			communictor.setfMemo(memo);
			
			dao.update(number,communictor);
			HttpSession session=request.getSession();
			List<Communictor> communictors=null;
			communictors=dao.getAll();
			if(communictors!=null)
			{
				session.setAttribute("communictor",communictors);
				return "COMM";
			}
			else{
				return ERROR;
			}
			
		}
		//进进入该入口后先进行判定查询的方式，然后在进行具体的查询，将其查询结果传递到session
		if(action.equals("querry")){
		 String province=request.getParameter("province");
		 String values=request.getParameter("value");
			HttpSession session=request.getSession();
			List<Communictor> communictors= dao.select(province,values);
			session.setAttribute("communictor", communictors);
			return "COMM";
		}
		return super.execute();
	}
}
